# coding: utf-8
# バイナリサンプル

import struct, binascii

# ----------------------------------------------------------------
# 値
# ----------------------------------------------------------------
values = (1, b'ab', 2.7)
# struct を作成
s = struct.Struct("I 2s f")

# ----------------------------------------------------------------
# パックする
# ----------------------------------------------------------------
packed_value     = s.pack(*values)

# ----------------------------------------------------------------
# アンパックする
# ----------------------------------------------------------------
unpacked_value   = s.unpack(packed_value)

# ----------------------------------------------------------------
# 表示
# ----------------------------------------------------------------
print("Format String :", s.format)
print("Original Value:", values)
print("Packed Value  :", packed_value)
print("0xPacked Value:", binascii.hexlify(packed_value))
print("Size          :", s.size, "bytes")
print("Unpacked Value:", unpacked_value)

""" 実行結果
Format String : b'I 2s f'
Original Value: (1, b'ab', 2.7)
Packed Value  : b'\x01\x00\x00\x00ab\x00\x00\xcd\xcc,@'
0xPacked Value: b'0100000061620000cdcc2c40'
Size          : 12 bytes
Unpacked Value: (1, b'ab', 2.700000047683716)
"""
